package com.dotflat.imprest.controller;

import com.dotflat.imprest.domain.OPImprest;
import com.dotflat.imprest.po.OPImprestAuditPO;
import com.dotflat.imprest.po.OPImprestLoanPO;
import com.dotflat.imprest.po.OPImprestModifyPO;
import com.dotflat.imprest.po.OPImprestPO;
import com.dotflat.imprest.service.OPImprestService;
import com.dotflat.rest.controller.RestfulBaseController;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.model.StatusResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.matt.foundation.constant.RequestConstant.TICKET;


@RestController
@RequestMapping("/v2/imprest")
@Slf4j
@Api(tags = "运营 - 备用金")
public class OPImprestController extends RestfulBaseController<OPImprest, OPImprestService, OPImprestPO> {

    @Autowired
    public void setBaseService(OPImprestService service){
        super.setBaseService(service);
    }

    @ApiOperation(value = "增加一条记录")
    @PostMapping()
    @Override
    public StatusResponse add(@RequestHeader(TICKET) String ticket, OPImprestPO param){
        StaffDTO staff = cache.getStaff(ticket);

        param.setCreatedBy(staff.getId());
        param.setCreatedByName(staff.getRealName());

        return super.add(ticket, param);
    }

    @ApiOperation(value = "审核一条记录")
    @PostMapping("audit")
    public StatusResponse audit(@RequestHeader(TICKET) String ticket, OPImprestAuditPO po){
        StaffDTO staff = cache.getStaff(ticket);

        OPImprestModifyPO param = new OPImprestModifyPO();

        param.setId(po.getId());
        param.setAuditReason(po.getAuditReason());
        param.setAudited(po.getAudited());

        param.setAuditAt(new Date());
        param.setAuditBy(staff.getId());
        param.setAuditByName(staff.getRealName());

        OPImprest ret = baseService.add(staff.getAreaId(), staff.getId(), param);

        List<OPImprest> tmp = new ArrayList<>();
        tmp.add(ret);

        return StatusResponse.success(tmp);
    }

    @ApiOperation(value = "放款一条记录")
    @PostMapping("loan")
    public StatusResponse loan(@RequestHeader(TICKET) String ticket, OPImprestLoanPO po){
        StaffDTO staff = cache.getStaff(ticket);

        OPImprestModifyPO param = new OPImprestModifyPO();

        param.setId(po.getId());
        param.setLoaned(po.getLoaned());

        param.setLoanedAt(new Date());
        param.setLoanedBy(staff.getId());
        param.setLoanedByName(staff.getRealName());

        OPImprest ret = baseService.add(staff.getAreaId(), staff.getId(), param);

        List<OPImprest> tmp = new ArrayList<>();
        tmp.add(ret);

        return StatusResponse.success(tmp);
    }



}
